{include file="public/head"/}

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">用户姓名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="username" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


            <!-- 原生 select 模板（推荐） -->
            <script type="text/html" id="TPL-select-primary">
                {{#  var levelList = d.levelList || []; }}
                <select name="level" class="layui-border select-demo-primary" lay-ignore>
                <option value="">未指定</option>
                    {{#  layui.each(levelList, function(i, v){ }}
                    <option value="{{= v.id }}" {{= v.id === d.level ? 'selected' : '' }}>{{= v.grade }}</option>
                {{#  }); }}
                </select> 
            </script>
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="more">
                    更多 
                    <i class="layui-icon layui-icon-down"></i>
                </a>
        </script>

        </div>
    </div>
    <script type="text/html" id="status-templet-switch">
        <input type="checkbox" name="status" value="{{= d.id }}" title="启用中|已关闭" lay-skin="switch" lay-filter="demo-templet-status" {{= d.status == 1 ? "checked" : "" }}>
    </script>
    <script>
        layui.use(['form', 'table','dropdown'], function () {
            var $ = layui.jquery,
                form = layui.form,
                dropdown = layui.dropdown,
                table = layui.table;

             table.render({
                elem: '#currentTableId',
                url: '{:url("index")}',
                method: 'POST',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {field: 'id', title: '编号',fixed: true, sort: true,style: "height:50px;"},
                    {field: 'avatar', title: '头像', },
                    {field: 'nickname', title: '昵称'},
                    {field: 'phone', title: '手机号码'},
                    {field: 'openid', title: 'openid', hide:true},
                    {field: 'sex', title: '性别',  sort: true},
                    {field: 'money', title: '余额',  sort: true},
                    {field: 'score', title: '积分',  sort: true},
                    {field: 'level', title: '级别', templet: '#TPL-select-primary'},
                    {field: 'status', title: '状态', align: 'center', width: 100, templet: '#status-templet-switch' },
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
                done: function(res, curr, count) {
                    var options = this;

                    // 获取当前行数据
                    table.getRowData = function(tableId, elem){
                        var index = $(elem).closest('tr').data('index');
                        return table.cache[tableId][index] || {};
                    };

                    // 原生 select 事件
                    var tableViewElem = this.elem.next();
                    tableViewElem.find('.select-demo-primary').on('change', function(){
                        var value = this.value; // 获取选中项 value
                        var data = table.getRowData(options.id, this); // 获取当前行数据(如 id 等字段，以作为数据修改的索引)
                        // 更新数据中对应的字段
                        data.level = value; 
                        // 修改用户等级
                        $.post('{:url("update")}',{id:data.id,field:'level',value:data.level},function(res) {
                                if(res.code > 0) {
                                   layer.msg(res.msg,{icon:1});
                                }else {
                                   layer.msg(res.msg,{icon: 2});
                                }
                        });
                    });
                },
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: {
                    theme: '#1E9FFF'
                },
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var data = data.field;
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1,
                        theme: '#1E9FFF'
                    }
                    , where: {
                        key: data.username,
                        group_id: data.auth_group
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '{:url("add")}',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '{:url("edit")}?id=' + data.id,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if(obj.event === 'more'){
                        // 更多 - 下拉菜单
                        dropdown.render({
                            elem: this, // 触发事件的 DOM 对象
                            show: true, // 外部事件触发即显示
                            data: [{
                            title: '余额积分',
                            id: 'detail'
                            },{
                            title: '删除',
                            id: 'del'
                            }],
                            click: function(menudata){
                            if(menudata.id === 'detail'){
                                var index = layer.open({
                                    title: '编辑其他',
                                    type: 2,
                                    shade: 0.2,
                                    maxmin: true,
                                    shadeClose: true,
                                    area: ['400px', '600px'],
                                    content: '{:url("set_money_intergral")}?id=' + data.id,
                                });
                                $(window).on("resize", function () {
                                    layer.full(index);
                                });
                                return false;
                                
                            } else if(menudata.id === 'del'){
                                layer.confirm('真的删除用户 [id: '+ data.id +'] 么', function(index){
                                    $.post('{:url("del")}',{id: data.id},function(res) {
                                        if(res.code > 0) {
                                            obj.del();
                                            layer.close(index);
                                        }else {
                                            layer.msg(res.msg, { time: 1000, icon: 2 });
                                            return false;
                                        }
                                     
                                    }); 
                                });
                            } 
                            },
                            align: 'right', // 右对齐弹出
                            style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                        })
                }
            });


            /**
              * 状态点击监听- 开关操作
            */
            form.on('switch(demo-templet-status)', function (obj) {
                var loading = layer.load(1, { shade: [0.1, '#fff'] });
                var id = this.value;
                $.post('{:url("admin/state")}', { 'id': id }, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        tableIn.reload();
                    } else {
                        layer.msg('操作失败！', { time: 1000, icon: 2 });
                        return false;
                    }
                });

            });

        });
    </script>

    {include file="public/footer"/}